package com.example.x.dnuifood_929dx.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.bean.UserBean;
import com.example.x.dnuifood_929dx.listener.onRetrofitListener;
import com.example.x.dnuifood_929dx.model.UserModel;

/**
 * Created by X on 2018/10/15.
 */

public class LoginActivity extends BaseActivity
        implements onRetrofitListener<UserBean> {
    private EditText username;
    private EditText userpass;
    private Button btn_login;
    private Button btn_reg;
    private SharedPreferences sp;
    private Switch sw;
    private String sessionID = "";
    Bundle bundle = new Bundle();//⽤用于Activity之间传输数据⽤用。
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//保存实例例状态的
//setContentView(R.layout.activity_login);
        initViews();//通过 findViewById ⽅方法进⾏行行控件绑定
        initEvents();//针对所有控件需要进⾏行行事件 处理理的控件进⾏行行注册，以便便onClick ⽅方法可以对其进⾏行行处理理
                sp=getSharedPreferences("userInfo",MODE_PRIVATE);//与BaseActivity⼀一致,不不⼀一致就获取不不到信息
        readSP();
    }
    //读取sp信息
    private void readSP() {
        String strname=sp.getString("name",null);
        String strpass=sp.getString("pass",null);
        Boolean remember=sp.getBoolean("remember",false);
        if (remember)
        {
            username.setText(strname);
            userpass.setText(strpass);
        }
        sw.setChecked(remember);
    }
    public void saveUser(String user_id){
        SharedPreferences sharedPreferences =
                getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器器
        editor.putString("username",username.getText().toString());
        editor.putString("userid",new UserBean().getUserid());
//int userid = Integer.parseInt(user_id);
        editor.putBoolean("remember",sw.isChecked());
        editor.putString("user_id",user_id);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交
    }
    void initViews() {
        setLayout(R.layout.activity_login);
        btn_reg = findViewById(R.id.btn_register);
        btn_login = findViewById(R.id.btn_login);
        username = findViewById(R.id.et_reg_user);
        userpass = findViewById(R.id.et_reg_password);
        sw=findViewById(R.id.switch1);
    }
    void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//onClick ⽅方法中处理理所有点击事件响应
                if(username.getText().toString().trim().equals("")||
                        userpass.getText().toString().trim().equals(""))
                {
                    Toast.makeText(LoginActivity.this,"⽤用户名和密码不不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    login();
// username.setText(getIntent().getStringExtra("username"));
//
// userpass.setText(getIntent().getStringExtra("password"));
                }
            }
        });//登录按钮的点击事件
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putString ("data", "test");
                Intent intent = new
                        Intent(LoginActivity.this,ReginsterActivity.class);
                startActivityForResult(intent,1);//在登录界⾯面启动注册界⾯面
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent
            data) {
        super.onActivityResult(requestCode, resultCode, data);
        String et_user = data.getStringExtra("user");
        username.setText(et_user);
    }//在登录界⾯面(LoginActivity)接收registerActivity中传来的参数，并显示在EditText 中。
    @Override
    void initData() {
    }
    public void login() {
        UserModel userModel = new UserModel();
        userModel.login(username.getText().toString().trim(),
                userpass.getText().toString().trim(), this);
//getText().trim()的作⽤用是在获得的⽂文本中除去空格.
    }
    @Override
    public void onSuccess(UserBean userid,int flag) {
        if (userid.getUserid().equals("0"))
        {
            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
        }
        else
        {
            saveUser(userid.getUserid());
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
// LoginActivity.this.finish();
        }
    }
    @Override
    public void onFailure(String string) {
        Toast.makeText(LoginActivity.this,
                string,Toast.LENGTH_SHORT).show();
    }
}



